import xlrd

import xlwt

import xlutils
import lxml
import pandas as pd
from selenium import webdriver
import time
from bs4 import BeautifulSoup
import requests
from requests.cookies import RequestsCookieJar
import urllib3
import re
import json

# 自动登录
drive = webdriver.Chrome()
drive.get('https://aregsr.vip/')
drive.find_element_by_id('goin').click()
drive.find_element_by_id('ls_username').send_keys('ggfygg')
drive.find_element_by_id('ls_password').send_keys('fhrAKMv@A4NXLkN')
drive.find_element_by_class_name('mem_login').click()
# selenium保存cookies
cookies = drive.get_cookies()
with open("cookies.txt", "w") as fp:
    json.dump(cookies, fp)

# requests读取cookies

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

s = requests.session()
s.verify = False
s.headers ={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36"}
# s.get("http://www.baidu.com")

# 这里我们使用cookie对象进行处理
jar = RequestsCookieJar()
with open("cookies.txt", "r") as fp:
    cookies = json.load(fp)
    for cookie in cookies:
        jar.set(cookie['name'], cookie['value'])

# df = pd.read_excel("test2.xlsx", header=None)

# # 或者如果你只是想创建一张空表
workbook = xlwt.Workbook(encoding='utf-8')
# # 创建一个sheet
worksheet = workbook.add_sheet('sheet')
# # 获取一个已存在的sheet
# worksheet = workbook.get_sheet('sheet')
#
# url = df[0].tolist()
# url = "https://aregsr.vip/thread-14827199-1-2.html"
# workbook = xlrd.open_workbook('tttt.xls')
# index = workbook.sheet_names()[0]
# sheet2 = workbook.sheet_by_name(index)

# 遍历
# nrows = sheet2.nrows
#
# for i in range(nrows):
#     print(sheet2.row_values(i))


r = s.get('https://aregsr.vip/thread-14809537-1-43.html', cookies=jar)
r.encoding = "utf-8"
pattern = re.compile(r'http://share4uspace.com(.*?)targe')
result = re.search(pattern, 'r.text')
# pattern = re.compile(r'：8888')
# result = re.match(pattern2, '：8888')
# pattern = re.compile(r'：118')
# result = re.match(pattern3, '：118')
if result:
# 获取一个已存在的sheet
# worksheet = workbook.get_sheet('sheet')
# # 写入一个值，括号内分别为行数、列数、内容
    worksheet.write(1, 1, result.group())
        # print('1成功')
else:
    print('匹配失败！')


workbook.save('memeda17.xls')
"""        pattern = re.compile(r'gZu2\(b1I0-Dc&amp;3t6')
        result = re.match(pattern, 'gZu2(b1I0-Dc&amp;3t6')


        if result:
            # 获取一个已存在的sheet
            worksheet.write(i, 1, 'gZu2(b1I0-Dc&3t6')
        else:
            pattern = re.compile(r'：8888')
            result = re.match(pattern, '：8888')
            if result:
                # 写入一个值，括号内分别为行数、列数、内容
                worksheet.write(i, 1, '8888')
                else:

                pattern = re.compile(r'：888')
                result = re.match(pattern, '：888')
                    if result:
                        # 获取一个已存在的sheet
                        worksheet = workbook.get_sheet('sheet')
                        # 写入一个值，括号内分别为行数、列数、内容
                        worksheet.write(i, 1, '888')
    """


# else:
#     pattern = re.compile(r'gZu2\(b1I0-Dc&amp;3t6')
#     result2 = re.match(pattern, 'gZu2(b1I0-Dc&amp;3t6')
#     if result2:
#       # 使用Match获得分组信息
#       print(result1.group())


# test_text2 = re.findall("解压密码",str(r))

# # 创建一个Workbook对象，相当于创建了一个Excel文件
# book3 = xlwt.Workbook(encoding="utf-8", style_compression=0)
# # 创建一个sheet对象，一个sheet对象对应Excel文件中的一张表格。
# sheet = book3.add_sheet('test01', cell_overwrite_ok=False)
#
# # 填入第一列
# for i in range(0, len(test_text)):
#     sheet.write(i, 0, test_text[i])
# book3.save('test3.xlsx')


# print(test_text2)
# except BaseException as msg:
#     print(msg)



















